﻿
Imports System.Threading
Imports std

Public Module starter

    Public Const defaultConfigfile As String = "starter.ini"
    Friend WithEvents config As configMonitor = Nothing
    Friend restart As Boolean = False

    Friend Function sourcepath() As String
        Return config.parameter(configNaming.starter.name, configNaming.starter.source _
                                , configNaming.defaultValue.starter.source)
    End Function

    Friend Function destpath() As String
        Return config.parameter(configNaming.starter.name, configNaming.starter.dest _
                                , configNaming.defaultValue.starter.dest)
    End Function

    Friend Sub sleepForQueue()
        Dim index As UInt64
        index = config.parameter(configNaming.starter.name, configNaming.starter.machineindex _
                                , configNaming.defaultValue.starter.machineindex)
        While index > 0
            index -= 1
            sleepEx()
        End While
    End Sub

    Private Sub sleepEx()
        Dim waittime As Int32
        waittime = config.parameter(configNaming.starter.name, configNaming.starter.waittime _
                                    , configNaming.defaultValue.starter.waittime)
        sleep(waittime)
    End Sub

    Friend Sub startService()
        On Error Resume Next

        sleepForQueue()

        Dim failed As Boolean
        'always retry when anything wrong, this is the doorway application

        Do
            failed = False

            'delete original app & deplay again
            If Not deleteDirectory(destpath(), maxInt32) OrElse _
                Not copyDirectory(sourcepath(), destpath(), False, maxInt32, True) Then
                raiseError("cannot delete original app & copy app from " + sourcepath() + ", ignore", _
                           errorHandle.errorType.exclamation)
                failed = True
            Else
                Dim app As String = Nothing
                Dim argument As String = Nothing
                app = config.parameter(Of String)(configNaming.starter.name, configNaming.starter.app)
                If isemptystring(app) Then
                    raiseError("do not have app value in configuration, ignore", errorHandle.errorType.exclamation)
                Else
                    argument = config.parameter(Of String)(configNaming.starter.name, configNaming.starter.argument _
                                                           , configNaming.defaultValue.starter.argument)
                    raiseError("start app " + app + ", with argument " + argument)
                    Dim proc As Process = Nothing
                    proc = New Process
                    app = fullpath(IO.Path.Combine(destpath(), app))
                    proc.StartInfo.FileName = app
                    proc.StartInfo.WorkingDirectory = destpath()
                    proc.StartInfo.Arguments = argument
                    If Not proc.Start Then
                        raiseError("cannot start process " + app + ", with argument " + argument _
                                    , errorHandle.errorType.exclamation)
                        failed = True
                    End If
                End If
            End If

            If failed Then
                'wait sometime if failed
                sleepEx()
            End If
        Loop Until Not failed
    End Sub

    Public Sub main(ByVal args() As String)
        errorHandle.initial(defaultLogFilename())

        Dim configfile As String = defaultConfigfile
        If args.Length > 0 Then
            configfile = args(0)
            raiseError("use argument0 " + configfile + "as config file, instead of default config file")
        End If

        config = New configMonitor()
        If config.open(configfile) Then
            raiseError("load configuration success, start service.")
            startService()
        Else
            raiseError("cannot load config file " + configfile, errorHandle.errorType.exclamation)
        End If
    End Sub

    Private Sub config_ConfigChanged() Handles config.ConfigChanged
        restart = True
    End Sub
End Module
